package com.acce.gxsc.page.supervisionReport.allDetail;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;

import com.acce.gxsc.R;
import com.acce.gxsc.common.App;
import com.acce.gxsc.common.DateUtils;
import com.acce.gxsc.common.FullyLinearLayoutManager;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.netBean.NetSupervisionBean;
import com.acce.sdk.User;
import com.acce.sdk.base.mvp.BaseMvpActivity;
import com.acce.sdk.utils.StringUtils;
import com.acce.sdk.widget.AcceBar;
import com.acce.sdk.widget.adapterrv.CommonAdapter;
import com.acce.sdk.widget.adapterrv.ViewHolder;

import java.text.MessageFormat;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by huxley on 2017/5/26.
 */

public class SupervisionReportAllDetailActivity extends BaseMvpActivity<SupervisionReportAllDetailView, SupervisionReportAllDetailPresenter> implements SupervisionReportAllDetailView {

    CommonAdapter<NetSupervisionBean> mAdapter;
    ArrayList<NetSupervisionBean>     data;
    boolean                           isAll;
    ArrayList<String>                 typeList;
    String                            parkArea;
    String dutyId;

    @Bind(R.id.rv_all)
    RecyclerView mRvAll;
    @Bind(R.id.accebar)
    AcceBar      mAccebar;
    @Bind(R.id.tv_is_all)
    TextView     mTvIsAll;
    @Bind(R.id.ll_all)
    LinearLayout mLlAll;
    @Bind(R.id.rv_supervision)
    RecyclerView mRvSupervision;
    @Bind(R.id.ll_supervision)
    LinearLayout mLlSupervision;
    @Bind(R.id.spinner_type)
    Spinner      mSpinnerType;
    @Bind(R.id.cb_park_area)
    CheckBox     mCbParkArea;
    @Bind(R.id.tv_duty_id)
    TextView     mTvDutyId;
    @Bind(R.id.tv_gate_name)
    TextView     mTvGateName;
    @Bind(R.id.tv_real_name)
    TextView     mTvRealName;
    @Bind(R.id.tv_print)
    TextView     mTvPrint;

    @Override
    protected SupervisionReportAllDetailPresenter createPresenter() {
        return new SupervisionReportAllDetailPresenter();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_supervision_report_all_detail);
        ButterKnife.bind(this);
        isAll = getIntent().getBooleanExtra("isAll", true);
        init();
        mPresenter.loadSupervisionReport(dutyId, getDiliveryType(), isAll ? "" : "1", parkArea);
    }

    private void init() {
        dutyId = getIntent().getStringExtra("dutyId");
        mInflater = LayoutInflater.from(this);
        mTvDutyId.setText(Tools.getString(dutyId));
        mTvGateName.setText(Tools.getString(App.gateName));
        mTvRealName.setText(Tools.getString(User.getInstance().getRealName()));
        data = new ArrayList<>();
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        if (isAll) {
            mLlAll.setVisibility(View.VISIBLE);
            mLlSupervision.setVisibility(View.GONE);
            mTvIsAll.setText("仅显示存在审核差异的车辆");
            mAccebar.setTitleText("当班监察--全部车辆清单");
            mRvAll.setLayoutManager(linearLayoutManager);
            mRvAll.setAdapter(mAdapter = new CommonAdapter<NetSupervisionBean>(this, R.layout.item_supervision_report_all, data) {
                @Override
                protected void convert(ViewHolder holder, final NetSupervisionBean supervisionBean, final int position) {
                    holder.setText(R.id.tv_id, Tools.getString(String.valueOf(position + 1)));
                    holder.setText(R.id.tv_dilivery_type, Tools.getString(supervisionBean.diliveryType));
                    holder.setText(R.id.tv_plate_num, Tools.getString3(supervisionBean.plateNum, supervisionBean.containNo, supervisionBean.hangNum));
                    holder.setText(R.id.tv_contact_name, Tools.getString4(supervisionBean.contactName, supervisionBean.underCompany));
                    holder.setText(R.id.tv_supervise_time, Tools.getTime(supervisionBean.superviseTime));
                    holder.setTextColor(R.id.tv_id, StringUtils.isEmpty(supervisionBean.superviseRemarks) ? Tools.getColor(R.color.stack) : Tools.getColor(R.color.red));
                    holder.setTextColor(R.id.tv_dilivery_type, StringUtils.isEmpty(supervisionBean.superviseRemarks) ? Tools.getColor(R.color.stack) : Tools.getColor(R.color.red));
                    holder.setTextColor(R.id.tv_plate_num, StringUtils.isEmpty(supervisionBean.superviseRemarks) ? Tools.getColor(R.color.stack) : Tools.getColor(R.color.red));
                    holder.setTextColor(R.id.tv_contact_name, StringUtils.isEmpty(supervisionBean.superviseRemarks) ? Tools.getColor(R.color.stack) : Tools.getColor(R.color.red));
                    holder.setTextColor(R.id.tv_supervise_time, StringUtils.isEmpty(supervisionBean.superviseRemarks) ? Tools.getColor(R.color.stack) : Tools.getColor(R.color.red));
                }
            });
        } else {
            mLlAll.setVisibility(View.GONE);
            mLlSupervision.setVisibility(View.VISIBLE);
            mTvIsAll.setText("显示全部审核车辆");
            mAccebar.setTitleText("当班监察--差异车辆清单");
            mRvSupervision.setLayoutManager(linearLayoutManager);
            mRvSupervision.setAdapter(mAdapter = new CommonAdapter<NetSupervisionBean>(this, R.layout.item_supervision_report_supervision, data) {
                @Override
                protected void convert(ViewHolder holder, final NetSupervisionBean supervisionBean, final int position) {
                    holder.setText(R.id.tv_id, Tools.getString(String.valueOf(position + 1)));
                    holder.setText(R.id.tv_plate_num, Tools.getString3(supervisionBean.plateNum, supervisionBean.containNo, supervisionBean.hangNum));
                    holder.setText(R.id.tv_supervise_remarks, Tools.getString(supervisionBean.superviseRemarks));
                    holder.setText(R.id.tv_supervise_time, Tools.getTime(supervisionBean.superviseTime));
                    holder.setTextColor(R.id.tv_id, StringUtils.isEmpty(supervisionBean.superviseRemarks) ? Tools.getColor(R.color.stack) : Tools.getColor(R.color.red));
                    holder.setTextColor(R.id.tv_plate_num, StringUtils.isEmpty(supervisionBean.superviseRemarks) ? Tools.getColor(R.color.stack) : Tools.getColor(R.color.red));
                    holder.setTextColor(R.id.tv_supervise_remarks, StringUtils.isEmpty(supervisionBean.superviseRemarks) ? Tools.getColor(R.color.stack) : Tools.getColor(R.color.red));
                    holder.setTextColor(R.id.tv_supervise_time, StringUtils.isEmpty(supervisionBean.superviseRemarks) ? Tools.getColor(R.color.stack) : Tools.getColor(R.color.red));
                }
            });
        }
        typeList = new ArrayList<>();
        typeList.add("全部登记类型");
        typeList.add("整柜-私人柜");
        typeList.add("整柜-月结公司");
        typeList.add("整柜-国冷、通泰、卓志");
        typeList.add("散货-国冷、通泰、卓志");
        typeList.add("散货-空运货");
        typeList.add("散货-精品货");
        typeList.add("整/散-国产货");
        typeList.add("登记-拼货车辆");
        typeList.add("登记-场地费");
        typeList.add("登记-机动位");
        typeList.add("登记-采购车辆");
        typeList.add("错单修改");
        mSpinnerType.setAdapter(new com.acce.sdk.widget.adapterlv.CommonAdapter<String>(this, R.layout.input_spinner_item, typeList) {
            @Override
            protected void convert(com.acce.sdk.widget.adapterlv.ViewHolder viewHolder, String s, int i) {
                viewHolder.setText(R.id.tv_content, s);
            }
        });
        mSpinnerType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                mPresenter.loadSupervisionReport(dutyId, getDiliveryType(), isAll ? "" : "1", parkArea);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        mCbParkArea.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                parkArea = b ? "内" : null;
                mPresenter.loadSupervisionReport(dutyId, getDiliveryType(), isAll ? "" : "1", parkArea);
            }
        });
    }

    @OnClick({R.id.ll_type, R.id.tv_print})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_type:
                Intent intent = new Intent(this, SupervisionReportAllDetailActivity.class);
                intent.putExtra("isAll", !isAll);
                startActivity(intent);
                finish();
                break;
            case R.id.tv_print:
                printOrder();
                break;
        }
    }

    LayoutInflater mInflater;
    private void printOrder() {
        ScrollView rootView = (ScrollView) mInflater.inflate(R.layout.supervision_report_all_detail_dialog_supervision, (ViewGroup) findViewById(R.id.dialog));
        final LinearLayout llContent = ButterKnife.findById(rootView, R.id.ll_content);
        final TextView tvClasses = ButterKnife.findById(rootView, R.id.tv_classes);
        final TextView tvCount = ButterKnife.findById(rootView, R.id.tv_count);
        final RecyclerView rv = ButterKnife.findById(rootView, R.id.rv);
        final TextView tvTollCollector = ButterKnife.findById(rootView, R.id.tv_toll_collector);
        final TextView tvDate = ButterKnife.findById(rootView, R.id.tv_date);
        final TextView tvDiliveryType = ButterKnife.findById(rootView, R.id.tv_dilivery_type);
        final TextView tvType = ButterKnife.findById(rootView, R.id.tv_type);
        final LinearLayout llSupervision = ButterKnife.findById(rootView, R.id.ll_supervision);
        final LinearLayout llAll = ButterKnife.findById(rootView, R.id.ll_all);
        tvClasses.setText(MessageFormat.format("班次：{0}", App.onTime));
        tvCount.setText(MessageFormat.format("总进场车辆：{0}车次", mAdapter.getDatas().size()));
        tvDiliveryType.setText(MessageFormat.format("送货分类：{0}", typeList.get(mSpinnerType.getSelectedItemPosition())));
        tvTollCollector.setText(MessageFormat.format("操作员：{0}", User.getInstance().getRealName()));
        tvType.setText(MessageFormat.format("{1} / {0}", isAll ? "--" : "存在差异车辆", mCbParkArea.isChecked() ? "新区内车辆" : "--"));
        tvDate.setText(DateUtils.getTime1());
        if (isAll) {
            llAll.setVisibility(View.VISIBLE);
            llSupervision.setVisibility(View.GONE);
        } else {
            llAll.setVisibility(View.GONE);
            llSupervision.setVisibility(View.VISIBLE);
        }

        rv.setLayoutManager(new FullyLinearLayoutManager(this));
        rv.setNestedScrollingEnabled(false);
        rv.setAdapter(new CommonAdapter<NetSupervisionBean>(this, isAll ? R.layout.supervision_report_all_detail_dialog_supervision_item_supervision : R.layout.supervision_report_all_detail_dialog_supervision_item_supervision_1, mAdapter.getDatas()) {
            @Override
            protected void convert(ViewHolder holder, final NetSupervisionBean saleSheet, final int position) {
                if (isAll) {
                    holder.setText(R.id.tv_id, String.valueOf(position + 1));
                    holder.setText(R.id.tv_acce_temp_1, Tools.getString(saleSheet.diliveryType));
                    holder.setText(R.id.tv_plate_num, Tools.getString3(saleSheet.plateNum, saleSheet.containNo, saleSheet.hangNum));
                    holder.setText(R.id.tv_contact_name, Tools.getString4(saleSheet.contactName, saleSheet.underCompany));
                    holder.setText(R.id.tv_create_time, Tools.getTime(saleSheet.superviseTime));
                } else {
                    holder.setText(R.id.tv_id, Tools.getString(String.valueOf(position + 1)));
                    holder.setText(R.id.tv_plate_num, Tools.getString3(saleSheet.plateNum, saleSheet.containNo, saleSheet.hangNum));
                    holder.setText(R.id.tv_supervise_remarks, Tools.getString(saleSheet.superviseRemarks));
                    holder.setText(R.id.tv_supervise_time, Tools.getTime(saleSheet.superviseTime));
                }
            }
        });
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog alertDialog = builder
//                .setTitle("是否打印交接班汇总报表")
                .setView(rootView)
                .setNegativeButton("取消", null)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mPresenter.printOrder(llContent);
                    }
                }).create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();
    }

    public String getDiliveryType() {
        int selectedItemPosition = mSpinnerType.getSelectedItemPosition();
        if (selectedItemPosition == 0) {
            return "";
        } else {
            return typeList.get(selectedItemPosition);
        }
    }

    @Override
    public void loadSupervisionReportSuccess(ArrayList<NetSupervisionBean> supervisionList) {
        mAdapter.getDatas().clear();
        if (supervisionList != null && !supervisionList.isEmpty()) {
            mAdapter.getDatas().addAll(supervisionList);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void printSuccess() {

    }
}
